﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;

namespace AutoCAD_CSharp_plug_in3.Chap04
{
    public static class Block1
    {
        private static readonly Dictionary<string, int> Dn = new Dictionary<string, int>
        {
            { "DN15", 22 },
            { "DN20", 27 },
            { "DN25", 34 },
            { "DN32", 42 },
            { "DN40", 48 },
            { "DN50", 60 },
            { "DN65", 76 },
            { "DN80", 89 },
            { "DN100", 114 },
            { "DN125", 140 },
            { "DN150", 168 }
        };

        private static int GetDnVal(string key)
        {
            return Dn.TryGetValue(key, out var val) ? val : Dn.Values.First();
        }

        /// <summary>
        ///     创建集流管段系列块
        /// </summary>
        /// <param name="blockName">定义块名称</param>
        /// <param name="lineLength">线长度</param>
        /// <param name="dn">型号</param>
        /// <returns>块定义</returns>
        private static BlockTableRecord Create(out string blockName, double lineLength, string dn)
        {
            blockName = "集流管段系列" + "_" + dn;
            var blockDef = new BlockTableRecord
            {
                Name = blockName,
                Origin = Point3d.Origin
            };

            const double direction = 0.0;

            var spacing = GetDnVal(dn) / 2.0;

            var dirVector = new Vector3d(Math.Cos(direction), Math.Sin(direction), 0);
            var perVector = new Vector3d(-Math.Sin(direction), Math.Cos(direction), 0);

            var startPoints = new Point3d[3];
            var endPoints = new Point3d[3];

            startPoints[0] = blockDef.Origin + perVector * spacing;
            endPoints[0] = startPoints[0] + dirVector * lineLength;

            startPoints[1] = blockDef.Origin;
            endPoints[1] = blockDef.Origin + dirVector * lineLength;

            startPoints[2] = blockDef.Origin - perVector * spacing;
            endPoints[2] = startPoints[2] + dirVector * lineLength;

            var leftLine = new Line(startPoints[0], endPoints[0])
            {
                Color = Color.FromColorIndex(ColorMethod.ByAci, 256)
            };
            blockDef.AppendEntity(leftLine);

            var middleLine = new Line(startPoints[1], endPoints[1])
            {
                Color = Color.FromColorIndex(ColorMethod.ByAci, 4) // 青色  
            };
            blockDef.AppendEntity(middleLine);

            var rightLine = new Line(startPoints[2], endPoints[2])
            {
                Color = Color.FromColorIndex(ColorMethod.ByAci, 256)
            };
            blockDef.AppendEntity(rightLine);
            return blockDef;
        }


        public static List<BlockTableRecord> CreateAll(string blockNamePrefix)
        {
            var blocks = new List<BlockTableRecord>();
            foreach (var pair in Dn)
            {
                var record = Create(out var blockName, 1000, pair.Key);
                record.Name = blockNamePrefix + blockName;

                blocks.Add(record);
            }

            return blocks;
        }
    }
}